package com.nccsjz.back.merch.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.nccsjz.back.merch.service.MerchService;
import com.nccsjz.back.news.service.NewsService;
import com.nccsjz.pojo.Merchant;
import com.nccsjz.pojo.News;
import com.nccsjz.base.BaseAction;
import com.nccsjz.utils.Pager;

@SuppressWarnings("serial")
public class MerchAction extends BaseAction {
	// 商户
	private Merchant merch;

	private MerchService service;
	private long mstat;
	private String mcode;
	public long getMstat() {
		return mstat;
	}

	public void setMstat(long mstat) {
		this.mstat = mstat;
	}

	public String getMcode() {
		return mcode;
	}

	public void setMcode(String mcode) {
		this.mcode = mcode;
	}

	// 存放分页信息
	private Pager pager;
	// 页码
	private int pageNo = 1;
	// 每页多少行
	private int pageSize = 40;
	// 判断标识
	private String flag;
	// 商户ID
	private long id;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Merchant getMerch() {
		return merch;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setMerch(Merchant merch) {
		this.merch = merch;
	}

	/**
	 * 加商户
	 * 
	 * @return
	 */
	public String add_Merch() {
		service = new MerchService();		
		try {		
			if(service.isExists(merch.getMname())){
				this.addActionError("商户已经存在!");
				return "register";
			}
			if (service.addMerch(merch)) {
				this.addActionMessage("添加成功！");
				return "success";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ERROR;
	}
	
	/**
	 * initPass_Merch方法用于初始化商户密码
	 * @return
	 * @throws SQLException 
	 */
	public String initPass_Merch() throws SQLException{
		service = new MerchService();
		try {
			service.initPassword(id);
			this.addActionMessage("商户密码已经初始化为6个1！");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.addActionMessage("密码初始化操作失败！");
		}		
		pager = service.getMerch(pageSize, pageNo);
		return "list";
	}

	/**
	 * 返回商户列表
	 * 
	 * @return
	 */
	public String list_Merch() {
		service = new MerchService();
		try {
			pager = service.getMerch(pageSize, pageNo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 前台查看商户信息具体内容。
	 * 
	 * @return
	 * @throws Exception
	 */
	public String viewCon_Merch() throws Exception {
		service = new MerchService();
		this.merch = service.getMerchById(id);
		return "view";
	}
	/**
	 * 后台商户审核信息具体内容。
	 * 
	 * @return
	 * @throws Exception
	 */
	public String viewCon1_Merch() throws Exception {
		service = new MerchService();
		this.merch = service.getMerchById(id);
		return "view1";
	}
	/**
	 * 前台，查看"更多"商户内容。
	 * 
	 * @return
	 * @throws Exception
	 */
	public String viewMore_Merch() throws Exception {
		service = new MerchService();
		pager = service.getMoreMerch(pageSize, pageNo);
		return "viewMore";
	}

	/**
	 * 注销商户
	 * 
	 * @throws SQLException
	 */
	public String del_Merch() throws SQLException {

		service = new MerchService();

		if (service.deleteMerch(id)) {
			return "listf";
		} else {
			return ERROR;
		}

	}

	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public String update_Merch() throws SQLException {

		service = new MerchService();

		if (service.updateMerch(merch)) {
			
			this.addActionMessage("修改成功！"); 
			
		}else{
			this.addActionMessage("修改失败！");
		} 
		
		return "success";

	}
	
	/**
	 *  登录用户修改
	 * @return
	 * @throws SQLException
	 */
	public String update1_Merch() throws SQLException {

		service = new MerchService();

		if (service.updateMerch1(merch)) {
			
			this.addActionMessage("修改成功！"); 
			
		}else{
			this.addActionMessage("修改失败！");
		} 
		
		return "success";

	}
	
	/**
	 * 按照商户名称查询
	 * @return
	 */
	public String search_Merch(){
		service = new MerchService();
		try {
			pager = service.getMerch(pageSize, pageNo,merch);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}
}
